# coding: utf-8 
# @Time : 2021/5/13 14:26
# @Author : shenshaoxiong
# @FileName: GetFieldData2db.py
# @Email   : 765105236@qq.com

from .Constant import HOST, USER, PASSWORD, DB, PORT, log_table, log_table_field_list
from .Funcs import get_mysql_data
from .Funcs import insert_mysql
from .Funcs import update_data
from .Funcs import get_now_time
from .Funcs import get_html_title_content
from .Funcs import get_text_date

def get_field_data_2_db(size=10):
    select_table = "news_detail_html"
    filter = "state=0"
    select_field_list = ["id", "source", "url", "html"]
    # 结果表
    insert_table = "news_dw"
    insert_vaules_list = []
    insert_field_list = ["id", "title", "content", "publish_time", "source", "url", "insert_time"]
    # 日志
    log_list = []
    # 更新数据
    update_values_list = []
    # 取数据
    task_data_list = get_mysql_data(host=HOST, user=USER, password=PASSWORD, db=DB, port=PORT, table=select_table,
                                    field_list=select_field_list, size=size, filter=filter)

    for task_data in task_data_list:
        id = task_data["id"]
        source = task_data["source"]
        url = task_data["url"]
        html = task_data["html"]
        html_title_content_dict = get_html_title_content(html)
        title = html_title_content_dict["title"]
        content = html_title_content_dict["content"]
        publish_time = get_text_date(html)
        insert_time = get_now_time()
        insert_vaules_list.append((id, title, content, publish_time, source, url, insert_time))
        update_values_list.append((1, id))

    insert_mysql(host=HOST, user=USER, password=PASSWORD, db=DB, port=PORT, table=insert_table,
                 field_list=insert_field_list, values_list=insert_vaules_list)
    update_sql = "UPDATE news_detail_html SET state = %s WHERE id = %s"
    update_data(host=HOST, user=USER, password=PASSWORD, db=DB, port=PORT, sql=update_sql,
                values_list=update_values_list)

# if __name__ == "__main__":
#     get_field_data_2_db(size=10)